// pages/admin/project/staff/staff.js
var app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    //tab切换  
    currentTab: 1,
    staffList: []

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var projectId = options.projectId
    //var projectId = 1013
    this.setData({
      projectId: projectId
    })
    this.getProjectStaff()
  },

  getProjectStaff: function (res) {
    wx.showLoading({
      title: '加载中',
    })
    var that = this
    var formData = {}
    formData.status = this.data.currentTab
    formData.projectId = this.data.projectId
    wx.request({
      url: app.host + '/projectStaff/search',
      header: {
        accessToken: app.accessToken
      },
      method: 'post',
      data: formData,
      complete: function (res) {
        wx.hideLoading()
        console.log(res)
        if (res.statusCode == 200) {
          if (res.data.code == 0) {
            that.setData({
              staffList: res.data.data
            })
          } else {
            that.setData({
              staffList: []
            })
          }
        } else {
          wx.showToast({
            title: '无法连接服务器',
            icon: 'none'
          })
        }
      }
    })
  },

  /** 更新人员状态 */
  updateStatus: function (res) {
    var that = this
    wx.request({
      url: app.host + '/projectStaff/auditStatus',
      header: {
        accessToken: app.accessToken,
      },
      data: {
        id: that.data.updateId,
        status: that.data.updateStatus
      },
      method: "post",
      complete: function (res) {
        console.log(res)
        if (res.statusCode == 200) {
          if (res.data.code == 0) {
            that.getProjectStaff()
          }
        }
      }
    })
  },

  // 顶部nav
  swichNav: function (e) {
    console.log("swichNav:", e)
    var that = this;
    if (this.data.currentTab === e.target.dataset.current) {
      return false;
    } else {
      that.setData({
        currentTab: e.target.dataset.current
      })
      that.getProjectStaff()
    }
  },

  /** 通过/不通过按钮事件 */
  auditStatus: function (e) {
    var status = e.currentTarget.dataset.status
    this.setData({
      updateStatus: e.currentTarget.dataset.status
    })
    this.updateStatus()
    this.setData({
      modalName: null
    })
  },

/** 设置普通人员/运营人员 */
  updateRoleTap:function(e){
    var that = this
    var role = e.currentTarget.dataset.role
    this.setData({
      updateRole: e.currentTarget.dataset.role,
      modalName: null
    })
    wx.request({
      url: app.host + '/projectStaff/updateRole',
      header: {
        accessToken: app.accessToken,
      },
      data: {
        id: that.data.updateId,
        userRole: that.data.updateRole
      },
      method: "post",
      complete: function (res) {
        console.log(res)
        if (res.statusCode == 200) {
          if (res.data.code == 0) {
            that.getProjectStaff()
          }
        }
      }
    })
  },


  showModal(e) {
    //设置当前操作的对象
    console.log(e)
    var updateId = e.currentTarget.dataset.id
    this.setData({
      modalName: e.currentTarget.dataset.target,
      updateId: updateId,
    })
  },

  hideModal(e) {
    console.log(e)
    this.setData({
      modalName: null
    })
  },


})